package com.bitcoin.scheduler.repository;

import com.bitcoin.scheduler.domain.BccBasicDataCrawl;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

import static com.bitcoin.scheduler.domain.Bcc.BathType.CRAWL_BASIC_INFO;

public interface BccBasicDataCrawlDao extends JpaRepository<BccBasicDataCrawl, String>, JpaSpecificationExecutor<BccBasicDataCrawl> {

    /**
     * 查询最新爬虫信息
     *
     * @return 最新爬虫信息
     */
    @Query(nativeQuery = true, value = "" +
        "SELECT crawl.* " +
        "FROM bcc_basic_data_crawl crawl " +
        "       INNER JOIN (SELECT batch_no " +
        "                   FROM bcc_batch batch " +
        "                          INNER JOIN (SELECT max(batch.batch_time_end) date FROM bcc_batch batch WHERE batch.batch_type = '" + CRAWL_BASIC_INFO + "') t on t.date = batch.batch_time_end) batch ON batch.batch_no = crawl.batch_no " +
        "       INNER JOIN bcc_basic_info bcc ON bcc.bcc_id = crawl.bcc_id " +
        "WHERE website_page_domain = 'manual_check'")
    List<BccBasicDataCrawl> findNewestCrawl();
}
